package com.ymatou.iapi.userservice.testcase;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.UserLogoRequestDtoBean;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.service.UserLogoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.Userinfoex;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.UserLogoWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;


/**
 * 用户上传，修改头像验证URL FreeMaker Template自动生成代码
 * 商家app-头像
 */
@TestSuite
public class Ts_UserLogoRequestDto {

	private static UserLogoRequestDtoBean lbb;
	private static UserLogoRequestDtoCall lbc;
	private static final String ADD = "Add";
	private static final String UPDATEACTION = "UpdateAction";
	private static final String SEARCH = "Search";
	private static final String DELETED = "Deleted";
	private static final String DEFAULT = "Default";
	private static JUserWapper logoWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改头像信息,兼容.net");
		logoWapper = new JUserWapper();
	}

	@Before
	public void caseUp() {
		lbb = new UserLogoRequestDtoBean();
		lbc = new UserLogoRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(lbc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserLogoRequestDto_001() {
		Logger.start(true,"添加头像验证URL成功");
		try {
			Map result = addLogoToUser();
			
			Userinfoex logo = logoWapper.selectUserinfoexByUid(Integer.valueOf(result.get("userId").toString())).get(0);
			Logger.verifyNotNull(logo, "验证logo信息不为null");
			Logger.verifyEquals(logo.getUserId().toString(), result.get("userId").toString(), "验证用户Id");
			Logger.verifyEquals(logo.getLogoUrl(), result.get("logoUrl"), "验证logo的url");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_UserLogoRequestDto_002() {
		Logger.start(true,"修改头像状态成功");
		try {
			Map result = addLogoToUser();
			String newLogo = "http://www.ymatou.com/"+TestDataManager.getRandomNum(4)+".jpg";
			lbb.setsLogoId(result.get("logoId").toString());
			lbb.setIUserId((Integer)result.get("userId"));
			lbb.setLogoOperatingAction(UPDATEACTION);
			lbb.setSLogoUrl(newLogo);
			lbb.setIAction(DEFAULT);
			lbc.setData(lbb);
			lbc.callService();

			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
//			Logger.verifyEquals("True", lbc.getString("Result"), "验证返回的result");
			
			Userinfoex logo = logoWapper.selectUserinfoexByUid(Integer.valueOf(result.get("userId").toString())).get(0);
			Logger.verifyNotNull(logo, "验证logo信息不为null");
			Logger.verifyEquals(logo.getUserId().toString(), result.get("userId").toString(), "验证用户Id");
			Logger.verifyEquals(logo.getLogoUrl(),newLogo, "验证logo的url");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_UserLogoRequestDto_003() {
		Logger.start(true,"删除头像成功");
		try {
			Map result = addLogoToUser();
			String newLogo = "";
			lbb.setsLogoId(result.get("logoId").toString());
			lbb.setIUserId((Integer)result.get("userId"));
			lbb.setLogoOperatingAction(UPDATEACTION);
			lbb.setSLogoUrl(newLogo);
			lbb.setIAction(DELETED);
			lbc.setData(lbb);
			lbc.callService();

			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
//			Logger.verifyEquals("True", lbc.getString("Result"), "验证返回的result");
			
			Userinfoex logo = logoWapper.selectUserinfoexByUid(Integer.valueOf(result.get("userId").toString())).get(0);
			Logger.verifyNotNull(logo, "验证logo信息不为null");
			Logger.verifyEquals(logo.getUserId().toString(), result.get("userId").toString(), "验证用户Id");
			Logger.verifyEquals(logo.getLogoUrl(), newLogo, "验证logo的url");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	@org.junit.Ignore
	public void Tc_UserLogoRequestDto_004() {
		Logger.start(false,"输入错误的操作，失败");
		try {
			//此异常新版本不管,根据setSLogoUrl设置
			Logger.comment("注册一个新用户");
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int userId = JUserCallService.createUserCall(cur);
			String logoUrl = "http://www.ymatou.com";
			
			Logger.comment("添加头像信息");
			lbb = new UserLogoRequestDtoBean();
			lbc = new UserLogoRequestDtoCall();
			lbb.setIUserId(Integer.valueOf(userId));
			lbb.setLogoOperatingAction(SEARCH);
			lbb.setIAction(DEFAULT);
			lbb.setSLogoUrl(logoUrl);
			lbc.setData(lbb);
			lbc.callService();
			
			Logger.verifyEquals(ECode.ERROR400, lbc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", lbc.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("错误的 Action", lbc.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserLogoRequestDto_005(){
		Logger.start(false, "未输入头像url，失败");
		try{
			Logger.comment("注册一个新用户");
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int userId = JUserCallService.createUserCall(cur);
			
			Logger.comment("添加头像信息");
			lbb = new UserLogoRequestDtoBean();
			lbc = new UserLogoRequestDtoCall();
			lbb.setIUserId(Integer.valueOf(userId));
			lbb.setLogoOperatingAction(ADD);
			lbb.setIAction(DEFAULT);
			lbc.setData(lbb);
			lbc.callService();
			
			Logger.verifyEquals("100", lbc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", lbc.getString("Success"), "验证操作是否成功");
			Logger.verifyIsContains("LogoUrl 未提供", lbc.getString("LastErrorMessage"), "验证LastErrorMessage信息");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserLogoRequestDto_006(){
		Logger.start(false, "输入错误的头像url，失败");
		try{
			Logger.comment("注册一个新用户");
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int userId = JUserCallService.createUserCall(cur);
			String logoUrl = "ymatou.com/12346";
			
			Logger.comment("添加头像信息");
			lbb = new UserLogoRequestDtoBean();
			lbc = new UserLogoRequestDtoCall();
			lbb.setIUserId(Integer.valueOf(userId));
			lbb.setLogoOperatingAction(ADD);
			lbb.setIAction(DEFAULT);
			lbb.setSLogoUrl(logoUrl);
			lbc.setData(lbb);
			lbc.callService();
			
			Logger.verifyEquals("100032", lbc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", lbc.getString("Success"), "验证操作是否成功");
			Logger.verifyIsContains("用户头像地址格式错误", lbc.getString("LastErrorMessage"), "验证Msg信息");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_UserLogoRequestDto_007() {
		Logger.start(true,"添加两次头像验证URL成功");
		try {
			Logger.comment("注册一个新用户");
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int userId = JUserCallService.createUserCall(cur);
			String logoUrl = "http://www.ymatou.com";
			
			Logger.comment("添加头像信息");
			lbb = new UserLogoRequestDtoBean();
			lbc = new UserLogoRequestDtoCall();
			lbb.setIUserId(Integer.valueOf(userId));
			lbb.setLogoOperatingAction(ADD);
			lbb.setIAction(DEFAULT);
			lbb.setSLogoUrl(logoUrl);
			lbc.setData(lbb);
			lbc.callService();
			lbb.setSLogoUrl(logoUrl+1);
			lbc.setData(lbb);
			lbc.callService();
			//重复添加
			Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
			Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
			String logoId = lbc.getString("Result");
			
			Map result = new HashMap<String, String>();
			result.put("logoUrl", logoUrl+1);
			result.put("logoId", logoId);
			result.put("userId", userId);
			
			Userinfoex logo = logoWapper.selectUserinfoexByUid(Integer.valueOf(result.get("userId").toString())).get(0);
			Logger.verifyNotNull(logo, "验证logo信息不为null");
			Logger.verifyEquals(logo.getUserId().toString(), result.get("userId").toString(), "验证用户Id");
			Logger.verifyEquals(logo.getLogoUrl(), result.get("logoUrl"), "验证logo的url");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserLogoRequestDto_008(){
		Logger.start(false, "输入的头像url不是ymatou.com的，失败");
		try{
			Logger.comment("注册一个新用户");
			CreateUserReq cur=new CreateUserReq();
			cur=JUserCallService.initCreateUserBean(cur);
			int userId = JUserCallService.createUserCall(cur);
			String logoUrl = "http://www.baidu.com/12346";
			
			Logger.comment("添加头像信息");
			lbb = new UserLogoRequestDtoBean();
			lbc = new UserLogoRequestDtoCall();
			lbb.setIUserId(Integer.valueOf(userId));
			lbb.setLogoOperatingAction(ADD);
			lbb.setIAction(DEFAULT);
			lbb.setSLogoUrl(logoUrl);
			lbc.setData(lbb);
			lbc.callService();
			
			Logger.verifyEquals("100032", lbc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", lbc.getString("Success"), "验证操作是否成功");
			Logger.verifyIsContains("用户头像地址格式错误", lbc.getString("LastErrorMessage"), "验证Msg信息");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	/**
	 * 给一个新用户添加头像
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private Map addLogoToUser() throws Exception{
		Logger.comment("注册一个新用户");
		CreateUserReq cur=new CreateUserReq();
		cur=JUserCallService.initCreateUserBean(cur);
		Integer userId = JUserCallService.createUserCall(cur);
		String logoUrl = "http://www.ymatou.com";
		
		Logger.comment("添加头像信息");
		lbb = new UserLogoRequestDtoBean();
		lbc = new UserLogoRequestDtoCall();
		lbb.setIUserId(userId);
		lbb.setLogoOperatingAction(ADD);
		lbb.setIAction(DEFAULT);
		lbb.setSLogoUrl(logoUrl);
		lbc.setData(lbb);
		lbc.callService();
		
		Logger.verifyEquals(ECode.SUCCESS, lbc.getResponseCode(), "验证Code");
		Logger.verifyEquals(true, lbc.getSuccess(), "验证Success");
		Logger.verifyEquals("200", lbc.getString("Result"), "验证Result");
		Logger.verifyEquals("ok", lbc.getString("SuccessMessage"), "验证SuccessMessage");
		String logoId = lbc.getString("Result");
		
		Map result = new HashMap<String, String>();
		result.put("logoUrl", logoUrl);
		result.put("logoId", logoId);
		result.put("userId", userId);
		return result;
	}
}